package com.huihuiyun.huihuiyun.server.controller.member.client;

import com.huihuiyun.huihuiyun.server.controller.member.client.viewobjects.HhyMemberAddressVO;
import com.huihuiyun.huihuiyun.server.domain.HhyMemberAddress;
import com.huihuiyun.huihuiyun.server.service.HhyMemberAddressService;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVO;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVOUtil;
import com.mark.util.JaxrsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Component
@Path(Const.context)
@Slf4j
public class MemberClientMemberAddressController {
    @Context
    private HttpServletResponse httpServletResponse;

    @Autowired
    private HhyMemberAddressService hhyMemberAddressService;

    /*******************
     * member favourite
     ******************/
    @GET
    @Path("/memberAddress/findMemberAddressListByMemberId")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO<List<HhyMemberAddressVO>> findMemberAddressListByMemberId(@QueryParam("memberId") String memberId) {
        try {
            List<HhyMemberAddress> hhyMemberAddressList = hhyMemberAddressService.findAllByMemberId(memberId);
            List<HhyMemberAddressVO> hhyMemberAddressVOList = ResultVOUtil.convertList(hhyMemberAddressList, HhyMemberAddressVO.class);
            return ResultVOUtil.success(hhyMemberAddressVOList);
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseInternalServerError(httpServletResponse);
            return ResultVOUtil.error("获取数据失败，请稍后重试");
        }
    }

    @POST
    @Path("/memberAddress/new")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO newBook(HhyMemberAddressVO hhyMemberAddressVO) {
        try {
            // 业务
            HhyMemberAddress hhyMemberAddress = ResultVOUtil.convert(hhyMemberAddressVO, HhyMemberAddress.class);
            hhyMemberAddress = hhyMemberAddressService.post(hhyMemberAddress);
            hhyMemberAddressVO = ResultVOUtil.convert(hhyMemberAddress, HhyMemberAddressVO.class);
            return ResultVOUtil.success(hhyMemberAddressVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("创建失败，请稍后重试");
        }
    }

    @POST
    @Path("/memberAddress/delete")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO deleteBook(HhyMemberAddressVO hhyMemberAddressVO) {
        try {
            String id = hhyMemberAddressVO.id;
            // 业务
            hhyMemberAddressService.delete(id);
            return ResultVOUtil.success();
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("删除失败，请稍后重试");
        }
    }

    @POST
    @Path("/memberAddress/update")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO updateBook(HhyMemberAddressVO hhyMemberAddressVO) {
        try {
            String id = hhyMemberAddressVO.id;
            // 业务
            hhyMemberAddressService.delete(id);
            return ResultVOUtil.success();
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("删除失败，请稍后重试");
        }
    }
}